/********************************************
 * 功能说明: OrganizationEmployeeRole实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeRoleDTO;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.model.OrganizationEmployee;
import com.spring.arch.uaa.model.OrganizationEmployeeRole;
import com.spring.arch.uaa.model.Role;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.repository.OrganizationEmployeeRoleRepository;
import com.spring.arch.uaa.repository.RoleRepository;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeRoleDTO;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001003005;

/**
 * OrganizationEmployeeRole实体与DTO之间接口数据转换实现
 *
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Component
public class WebOrganizationEmployeeRoleConverter extends AbstractConverter<OrganizationEmployeeRole,
        WebOrganizationEmployeeRoleDTO> {

    @Autowired
    private OrganizationEmployeeRoleRepository organizationEmployeeRoleRepository;

    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public OrganizationEmployeeRole toModel(@NonNull final WebOrganizationEmployeeRoleDTO dto) {
        if(dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebOrganizationEmployeeRoleDTO toDTO(@NonNull final OrganizationEmployeeRole model) {
        final WebOrganizationEmployeeRoleDTO dto = new WebOrganizationEmployeeRoleDTO();

        dto.setId(model.getId());
        dto.setRole(model.getRole().getId());
        dto.setName(model.getRole().getName());

        return dto;
    }

    /**
     * 构建新Model
     */
    private OrganizationEmployeeRole constructModel(final WebOrganizationEmployeeRoleDTO dto) {
        final OrganizationEmployeeRole model = new OrganizationEmployeeRole();
        this.dtoToModel(dto, model);
        return model;
    }

    /**
     * 更新Model
     */
    private OrganizationEmployeeRole updateModel(final WebOrganizationEmployeeRoleDTO dto) {
        final OrganizationEmployeeRole model = this.organizationEmployeeRoleRepository.findById(dto.getId())
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001003005, String.valueOf(dto.getId())));
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebOrganizationEmployeeRoleDTO dto, final OrganizationEmployeeRole model) {
        UUID empId = dto.getOrganizationEmployee();
        final OrganizationEmployee organizationEmployee = this.organizationEmployeeRepository.findById(empId)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001003004, String.valueOf(empId)));

        UUID roleId = dto.getRole();
        final Role role = this.roleRepository.findById(roleId).orElseThrow(() -> ExceptionHolder.
                serviceException(UaaErrorCode.E2001006001, String.valueOf(roleId)));

        model.setOrganizationEmployee(organizationEmployee);
        model.setRole(role);
        model.setTenant(organizationEmployee.getOrganization().getTenant());
        model.setOrganization(organizationEmployee.getOrganization());
        model.setProject(organizationEmployee.getOrganization().getProject());
    }
}
